#!/usr/bin/env bash
set -eu

case ${1:-} in
  '')
    . $(dirname $0)/util
    gogo_version=$(awk '$1 == "github.com/gogo/protobuf" { print $2 }' go.mod)
    buildxCmd build \
      --target validate \
      --build-arg "GOGO_VERSION=$gogo_version" \
      --file ./hack/dockerfiles/generated-files.Dockerfile \
      .
    ;;
  check)
    diffs="$(git status --porcelain -- **/*.pb.go 2>/dev/null)"
    set +x
    if [ "$diffs" ]; then
      {
        echo 'The result of "go generate" differs'
        echo
        echo "$diffs"
        echo
        echo 'Please update with "make generated-files"'
        echo
      } >&2
      exit 1
    fi
    echo 'Congratulations! All auto generated files are correct.'
    ;;
esac
